 /**
 * Description: excal
 * @author:jacky
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2019-03-18   jacky       1.0.0     Initial
 */
package com.metro.demo.excal.controller;

import com.github.pagehelper.PageInfo;
import com.metro.core.annotation.BusinessDesc;
import com.metro.core.page.Page;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.QueryParams;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

import com.metro.demo.excal.service.inf.IExcalTestService;
import com.metro.demo.excal.beans.vo.ExcalTest;
import com.metro.demo.web.controller.BaseController;
 /**
 * excal<br>
 *
 * @author:jacky
 * @since:1.0.0
 * @version [1.0.0, 2019-03-18]
 */
@Controller
@RequestMapping("/excalTest")
public class ExcalTestController extends BaseController {
    //菜单编码
    private static final String menuCode = "EXCAL_TEST";
    //模块描述
    private static final String MODULE_DESC = "excal";
    @Autowired
    private IExcalTestService excaltestService;
    @Override
    protected String getViewDir() {
        return "excal/";
    }
    
    /**
     * 去查询excal
     * 
     * @param excaltest excal页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2019-03-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询excal列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "list", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView list(ExcalTest excaltest) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.setViewName(getView("excaltest_list"));
            mav.addObject("searchEntity", excaltest);
        } catch (Exception ex) {
            log.error("查询excal出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询excal
     *
     * @param excaltest excal页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2019-03-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询excal列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolist", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolist(ExcalTest excaltest, Page page, QueryParams<ExcalTest> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setPaging(page);
            queryParams.setEntity(excaltest);
            PageInfo<ExcalTest> pageInfo = excaltestService.queryForPage(queryParams);
            jr.setData(pageInfo);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询excal出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 去新增excal
     * 
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2019-03-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去新增excal")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "create")
    public ModelAndView createForm(Model model, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            initCreateOrUpdateFrom(mav, "create");
        } catch (Exception ex) {
            log.error("去新增excal出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }
    
    /**
     * 初始化新增修改excal所需参数
     * @author jacky
     */
    private void initCreateOrUpdateFrom(ModelAndView mav, String action){
        mav.addObject("action", action);
        mav.setViewName(getView("excaltest_form"));
    }
    
    /**
     * 新增excal
     * 
     * @param entity excal页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2019-03-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增excal")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult saveExcalTest(ExcalTest entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = excaltestService.save(entity);
            jr.setResult(res);
            log.debug("新增excal成功【{}】", entity);
        } catch (Exception ex) {
            log.error("新增excal出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 删除excal
     * 
     * @param id excal页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2019-03-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除excal")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable String id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除excalid={}",id);
            IResult codeMsg = excaltestService.deleteByIds(id);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除excal出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去修改excal
     * 
     * @param id excal页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2019-03-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去修改excal")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "edit/{id}", method = { RequestMethod.GET})
    public ModelAndView updateForm(@PathVariable Long id, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            ExcalTest entity = excaltestService.getById(id);
            mav.addObject("entity", entity);
            initCreateOrUpdateFrom(mav,"edit");
        } catch (Exception ex) {
            log.error("去修改excal", ex);
            mav.setView(new RedirectView("list"));
        }
        return mav;
    }

    /**
     * 修改excal
     * 
     * @param entity excal页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2019-03-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改excal")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult updateExcalTest(ExcalTest entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = excaltestService.update(entity);
            jr.setResult(res);
            log.debug("修改excal成功【{}】", entity);
        } catch (Exception ex) {
            log.error("修改excal出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去查看excal
     * 
     * @param id excal页面表单对象唯一标识
     * @return 结果视图
     * @author jacky
     * @since [1.0.0]
     * @version [1.0.0, 2019-03-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看excal详情")
    @RequiresPermissions(menuCode + ":view")
    @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
    public ModelAndView info(@PathVariable Long id) {
        ModelAndView mav= new ModelAndView();
        try {
            mav.addObject("entity", excaltestService.getById(id));
            mav.setViewName(getView("excaltest_info"));
        } catch (Exception ex) {
            log.error("查看excal出错", ex);
        }
        return mav;
        
    }
    
}
